package com.lamda.zhekou.controller;

import com.lamda.zhekou.exception.ZheKouException;
import com.lamda.zhekou.manager.PageModel;
import com.lamda.zhekou.manager.UserManager;
import com.lamda.zhekou.model.User;
import com.lamda.zhekou.vo.ResultVo;
import com.lamda.zhekou.vo.UserVpsSelectVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by MJiao on 15/8/14.
 */
@Controller
public class UserController {

    @Autowired
    private UserManager userManager;


    @RequestMapping(value = "/getUserByFilter")
    @ResponseBody
    public List<UserVpsSelectVo> recordVps(@RequestParam(value = "keyWord", required = false) String keyWord) {
        if (StringUtils.isEmpty(keyWord)) {
            return new ArrayList<>();
        }
        return this.userManager.getUsersByFyEmailFilter(keyWord);

    }


    @RequestMapping(value = "/userList", method = RequestMethod.GET)
    public String userList(Model model,
                           @RequestParam(value = "pageNo", required = false, defaultValue = "1") int pageNo,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
                           @RequestParam(value = "asc", required = false, defaultValue = "false") boolean asc) {

        PageModel<User> pageModel = this.userManager.paging(pageNo, pageSize, asc);
        model.addAttribute("pageModel", pageModel);
        return "userList";
    }

    @RequestMapping(value = "/admin", method = RequestMethod.GET)
    public String setAdmin(Model model) {

        List<User> adminList = this.userManager.getAllAdmins();
        List<User> commonUserList = this.userManager.findAllCommonUser();

        model.addAttribute("commonUserList", commonUserList);

        model.addAttribute("adminList", adminList);
        return "adminList";
    }
    @RequestMapping(value = "/setAdmin", method = RequestMethod.GET)
    public String setUserToAdmin(Model model) {

        List<User> adminList = this.userManager.findAllCommonUser();
        model.addAttribute("adminList", adminList);
        return "setAdmin";
    }



    @RequestMapping(value = "/doSetUserToAdmin")
    @ResponseBody
    public ResultVo setUserToAdmin(Model model, @RequestParam(value = "userId", required = true) Long userId) {
        ResultVo resultVo = new ResultVo();

        try {
            this.userManager.setUserToAdmin(userId);
            resultVo.setSucc(true);
            resultVo.setMsg("设置改成功");
        } catch (ZheKouException e) {
            e.printStackTrace();
            resultVo.setSucc(false);
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }

    @RequestMapping(value = "/cancelUserAdmin")
    @ResponseBody
    public ResultVo cancelUserAdmin(Model model, @RequestParam(value = "userId", required = true) Long userId) {
        ResultVo resultVo = new ResultVo();

        try {
            this.userManager. cancelUserAdmin(userId);
            resultVo.setSucc(true);
            resultVo.setMsg("取消成功");
        } catch (ZheKouException e) {
            e.printStackTrace();
            resultVo.setSucc(false);
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Model model) {
        model.addAttribute("title", "登陆");
        return "login";
    }

    @RequestMapping(value = "/modifyPassword", method = RequestMethod.GET)
    public String modifyPassword(Model model) {
        model.addAttribute("title", "登陆");
        return "modifyPassword";
    }

    @RequestMapping(value = "/doModifyPasswod")
    @ResponseBody
    public ResultVo doModifyPasswod(Model model,
                                  HttpServletRequest httpServletRequest,
                                  @RequestParam(value = "oldPassword", required = true) String oldPassword,
                                  @RequestParam(value = "password", required = true) String password,
                                  @RequestParam(value = "confirmPassword", required = true) String confirmPassword) {
        ResultVo resultVo = new ResultVo();
        User user = (User) httpServletRequest.getSession().getAttribute("loginedUser");
        try {
            this.userManager.modifyPassword(user.getId(), oldPassword, password, confirmPassword);
            resultVo.setSucc(true);
            resultVo.setMsg("密码修改成功");
        } catch (ZheKouException e) {
            e.printStackTrace();
            resultVo.setSucc(false);
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }

    @RequestMapping(value = "/profile", method = RequestMethod.GET)
    public String profile(Model model) {
        model.addAttribute("title", "登陆");
        return "/";
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public ModelAndView logout(HttpServletRequest httpServletRequest) {
        httpServletRequest.getSession().removeAttribute("loginedUser");
        return new ModelAndView("redirect:/");
    }

    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo doLogin(HttpServletRequest httpServletRequest, Model model,
                            @RequestParam(value = "account", required = true) String account,
                            @RequestParam(value = "password", required = true) String password) {
        ResultVo resultVo = new ResultVo();
        try {
            User user = this.userManager.login(account, password);
            resultVo.setMsg("登陆成功");
            resultVo.setSucc(true);
            //将当前登陆用户保存到session中
            httpServletRequest.getSession().setAttribute("loginedUser", user);

        } catch (ZheKouException e) {
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String register(Model model) {
        model.addAttribute("title", "注册");
        return "register";
    }

    @RequestMapping(value = "/doRegister", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo doRegister(HttpServletRequest httpServletRequest, Model model,
                               @RequestParam(value = "account", required = true) String account,
                               @RequestParam(value = "nickName", required = true) String nickName,
                               @RequestParam(value = "passwd1", required = true) String passwd1,
                               @RequestParam(value = "passwd2", required = true) String passwd2,
                               @RequestParam(value = "phoneNum", required = true) String phoneNum,
                               @RequestParam(value = "QQ", required = true) String qq)
//                               @RequestParam(value = "inviteCode", required = true) String inviteCode,
                              // @RequestParam(value = "volidateCode", required = true) String volidateCode)
                              {

        ResultVo resultVo = new ResultVo();

//        if (!volidateCode.equals(httpServletRequest.getSession().getAttribute("volidateCode").toString())) {
//            resultVo.setMsg("验证码不正确");
//            return resultVo;
//        }


        User user = new User();
        user.setEmail(account);
        user.setNickname(nickName);
        user.setPassword(passwd1);
        user.setQq(qq);
        user.setTelephone(phoneNum);
//        user.setFilledInviteCode("root");

        try {
            this.userManager.register(user);
            resultVo.setMsg("注册成功");
            resultVo.setSucc(true);
        } catch (ZheKouException e) {
            resultVo.setMsg(e.getMessage());
        }

        return resultVo;
    }
}
